/**
 * FileName: UserController
 * Author: TangZaoBang
 * Date: 2023/04/29 22:47
 * Description:
 * <p>
 * History:
 * <author> TangZaoBang
 * <time> 2023/04/29 22:47
 * <version> 1.0.0
 * <desc> 版本描述
 */

package com.bosssoft.quickstart.demo.controller;

import com.bosssoft.quickstart.demo.api.UserApi;
import com.bosssoft.quickstart.demo.pojo.dto.IdVersion;
import com.bosssoft.quickstart.demo.pojo.dto.UserDTO;
import com.bosssoft.quickstart.demo.pojo.query.UserQuery;
import com.bosssoft.quickstart.demo.pojo.vo.CommonPage;
import com.bosssoft.quickstart.demo.pojo.vo.CommonResponse;
import com.bosssoft.quickstart.demo.service.UserService;
import com.bosssoft.quickstart.demo.utils.CommonResponseUtils;
import com.bosssoft.quickstart.demo.utils.MyBeanUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @Class UserController
 * @Description
 * @Author TangZaoBang
 * @Date 2023/04/29 22:47
 * @Version
 */
@RestController
public class UserController implements UserApi {
    /**
     * 用户service 包括了用户的所有的业务
     */
    @Autowired
    private UserService userService;
    /**
     * 增加方法，
     * @author TangZaoBang
     * @date 2023/04/29 22:55
     * @param userDTO 
     * @return com.bosssoft.quickstart.demo.pojo.vo.CommonResponse<java.lang.Boolean> 
     */
    @Override
    public CommonResponse<Boolean> add(@NotNull @Valid @ApiParam @RequestBody UserDTO userDTO) {
        return CommonResponseUtils.booleanResult(userService.add(userDTO));
    }

    /**
     * 通过id 删除User
     *
     * @author TangZaoBang
     * @date 2023/04/30 00:22
     * @param id 要删除的User的id
     * @return com.bosssoft.quickstart.demo.pojo.vo.CommonResponse<java.lang.Boolean>
     */
    @Override
    public CommonResponse<Boolean> removeUserById( @RequestParam("id") @NotNull Long id) {
        return CommonResponseUtils.booleanResult(userService.removeUserById(id));
    }

    @Override
    public CommonResponse<Boolean> removeUserByIdAndVersion(@NotNull @RequestParam("id") Long id, @NotNull@RequestParam("version") Long version) {
        return CommonResponseUtils.booleanResult(userService.removeUserByIdAndVersion(id,version));
    }

    @Override
    public CommonResponse<Boolean> updateUser(@NotNull @Valid @RequestBody UserDTO userDTO) {
        return CommonResponseUtils.booleanResult(userService.updateUser(userDTO));
    }


    /**
     * findUserById 通过Id查询User
     * @author TangZaoBang
     * @date 2023/04/30 11:28
     * @param id 用户的id
     * @return com.bosssoft.quickstart.demo.pojo.vo.CommonResponse<com.bosssoft.quickstart.demo.pojo.dto.UserDTO>
     */
    @Override
    public CommonResponse<UserDTO> findUserById(@NotNull @PathVariable Long id) {
        return CommonResponseUtils.success(MyBeanUtils.copyProperties(userService.findUserById(id),UserDTO::new));
    }

    @Override
    public CommonResponse<Boolean> removeUsers(@NotEmpty @RequestBody IdVersion[] idVersions) {
        return CommonResponseUtils.booleanResult(userService.removeUsers(idVersions));
    }

    @Override
    public CommonResponse<CommonPage<UserDTO>> search(@NotNull @Valid UserQuery userQuery) {
        return CommonResponseUtils.success(userService.search(userQuery));
    }
}
